

import os
import xml.etree.ElementTree as ET
import shutil

#程序功能：批量修改VOC数据集中xml标签文件的标签名称
def changelabelname(inputpath,img_path,move_path1,move_path2):
    listdir = os.listdir(inputpath)
    for file in listdir:
        print(file)
        image_name=file.replace('xml','jpg')
        if file.endswith('xml'):
            file = os.path.join(inputpath,file)
            tree = ET.parse(file)
            root = tree.getroot()
            for object1 in root.findall('object'):
                for sku in object1.findall('name'):           #查找需要修改的名称
                    if not os.path.exists(os.path.join(img_path,image_name)):
                        continue
                    elif (sku.text == 'bj_bpmh'):
                        print(sku.text)
                        shutil.move(os.path.join(img_path,image_name),os.path.join(move_path1,image_name))
                        break
                    elif (sku.text == 'bj' or sku.text == 'bjdsyc' or sku.text == 'bpps' or sku.text == 'bj_wkps'):
                        shutil.move(os.path.join(img_path,image_name),os.path.join(move_path2,image_name))
                        break


        else:
            pass

if __name__ == '__main__':
    inputpath=f'/home/hegang/datas/gm1/2/xmls'  #此处替换为自己的路径
    img_path='/home/hegang/datas/gm1/1'
    move_path1='/home/hegang/datas/gm1/bjmh'
    move_path2='/home/hegang/datas/gm1/bjds'
    changelabelname(inputpath,img_path,move_path1,move_path2)